| No. | Answer | Remark | |
|---|---|---|---|
| 1 | c | Prints: 195ab | The operands of the first addition operator are both char literals and are evaluated as integral numeric values. The right hand operand of the second addition operator is a String so the result of the first addition operator is promoted to a String type. The rest of the operands within the expression will later be promoted to String values and the addition operators will be evaluated as String concatenation operators. |
| 2 | e | Prints: -0.0,0.0,0.0 | A positive value multiplied by a negative value is negative. A negative value multiplied by a negative value is positive. A positive value added to a negative value of equal magnitude is zero. |
| 3 | c d | Compiler error at 3 Compiler error at 4 | This question demonstrates a variety of assignment conversions. The compiler will implicitly do a narrowing conversion for an assignment statement if the right hand operand is a compile time constant of type byte, short, char, or int and the value falls within the range of the variable on the left and if the variable on the left is of type byte, short, or char. In this case, variables s1 and c1 are not compile time constants so the compiler will not do an implicit narrowing conversion. However, variabls s2 and c2 are compile time constants that fall within the range of the left hand operand. |
| 4 | d | Prints: 5 | If the left hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to an int. If the promoted type of the left hand operand is of type int, then the shift operator only uses the least significant five bits of the right hand operand and the actual shift distance is always less than 32 bits. Therefore, if the right hand operand is 32 then the shift distance is zero because all five of the least significant bits are zero. Note: If the type of the left hand operand is long, then the least significant six bits of the right hand operand are used. |
| 5 | c | Prints: -1 | If the left hand operand of the shift operator is of type byte, short, or char then the left operand is promoted to a 32 bit int and all four bytes are shifted. The result is of type int. The compound assignment operator includes an implicit cast to the type of the left hand operand. Therefore, the result of the shift operation, an int, is cast to a byte before the assignment to variable b. The result of the cast is the contents of the least significant byte. In this case, all eight bits are set to one so the result is a negative one in two's compliment format. |
| 6 | a | Prints: float,float | An argument of type char can be widened to an int, long, float or double. An argument of type long can be widened to a float or a double. Since the float type is the most specific type available here it selected before double. |
| 7 | d | Prints: true,false,false | When two is added to Integer.MAX_VALUE the result is an integer with the most significant bit set to one and the least significant bit set to one and all the bits in between set to zero. The same is true for the two's compliment representation of the most negative integer plus one. |
| 8 | d | Prints: true, true, true | Negative infinity is rounded to the most negative value of type int or long. Positive infinity is rounded to the most positive value of type int or long. NaN is converted to zero. |
| 9 | g | Compiles and runs without error. | The null literal is converted to an int array type. All array types implement the Cloneable interface and may therefore be assigned to a reference of type Cloneable. The int array object referenced by the Cloneable reference c can be assigned to a reference of the int array type. |
| 10 | d | Prints: 0, -1, 0, 65535 | The most negative value of type int is represented as 0x80000000 and becomes 0x0000 when converted to a short or char. The most positive value of type int is represented as 0x7FFFFFFF and becomes 0xFFFF when converted to a short or char. Since type short is a signed type the value 0xFFFF is interpreted as the two's compliment representation of negative one. Type char is unsigned so 0xFFFF is interpreted as the maximum positive value of type char. Please note that the difference between a signed value and an unsigned value is the fact that the left most bit of a signed value is the sign bit. If the sign bit is set to one then the value is interpreted as a negative value in two's compliment format. |
| 11 | b | Prints: 1,0 | The statement contains an addition operation. The left operand is a postfix operation and it is evaluated first. The result of the postfix operation is zero, but variable i is incremented as a side effect. The right hand operand of the addition operation is the result of method m. As a side effect, method m prints the current value of variable i which is one. Method m then returns the value zero. The result of the addition operation is zero and that is the value that is assigned to variable i. |
| 12 | a | Prints: 1,2,3,4,1 | Java evaluates expressions from left to right while respecting operator precedence. j = 1 + (2 * 3) + 4 == 11 |
| 13 | c | Prints: m3Cm2Bm1A | Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, the most specific version of method m3 is the one that declares a parameter of type C. The most specific version of m2 is the one that declares a parameter of type B. |
| 14 | b | Prints: B | Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, type B is more specific than type A because B extends A. |
| 15 | e | Compile-time error. | A method argument will not be implicitly narrowed from type double to type float. |
| 16 | g | Compiles and runs without error. | The compound assignment operators include an implicit cast to the type of the left hand operand. Therefore, an explicit cast is not necessary. |
| 17 | c | Prints: -2, 3, 0, 2, 2, 5 | |